package com.ysd.lis.service.bac.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.*;
import com.ysd.lis.entity.bac.*;
import com.ysd.lis.entity.lab.*;
import com.ysd.lis.mapper.bac.*;
import com.ysd.lis.mapper.lab.LabMaininfoMapper;
import com.ysd.lis.mapper.sys.SysInterfaceMapper;
import com.ysd.lis.request.bac.BacDrugDto;
import com.ysd.lis.request.bac.BacTestCountDto;
import com.ysd.lis.request.bac.BacTestCountVo;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.bac.BacDrugTestResultService;
import com.ysd.lis.service.bac.BacTestResultService;
import com.ysd.lis.service.common.RedisSysParamHelper;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-03-21
 */
@Service
public class BacTestResultServiceImpl extends ServiceImpl<BacTestResultMapper, BacTestResult> implements BacTestResultService {

    @Autowired
    BacTestResultMapper bacTestResultMapper;
    @Autowired
    LabMaininfoMapper labMaininfoMapper;
    @Autowired
    BacStainingResultMapper bacStainingResultMapper;
    @Autowired
    BacDrugTestResultMapper bacDrugTestResultMapper;
    @Autowired
    BacBreakPointCalcServiceImpl bacBreakPointCalcService;
    @Autowired
    BacDrugTestResultService bacDrugTestResultService;
    @Autowired
    BacTestWorkFlowMapper bacTestWorkFlowMapper;
    @Autowired
    SysInterfaceMapper sysInterfaceMapper;

    @Override
    public Result findBacTestResultList(BacTestResult bacTestResult) {
        List<BacTestResult> list = findBacTestResultArray(bacTestResult);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public Result getAlarmResult(LabCriticalReportDto bacTestResult) {

        Timestamp startTime = Timestamp.valueOf(bacTestResult.getSampleda().substring(0, 10) + " 00:00:00");
        Timestamp endTime = Timestamp.valueOf(bacTestResult.getSampleda().substring(0, 10) + " 23:59:59");

        MPJLambdaWrapper<BacTestResult> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(BacTestResult.class);
        lambdaQueryWrapper.selectAs("b1", LabCriticalReport::getId, BacTestResult::getCultical3Id);
        lambdaQueryWrapper.selectAs("b2", LabCriticalReport::getId, BacTestResult::getCultical4Id);
        lambdaQueryWrapper.leftJoin(LabCriticalReport.class, "b1", on -> on
                .eq(LabCriticalReport::getResultId, BacTestResult::getId)//防止格式化
                .eq(LabCriticalReport::getCriticalItemType, 3)
        );
        lambdaQueryWrapper.leftJoin(LabCriticalReport.class, "b2", on -> on
                .eq(LabCriticalReport::getResultId, BacTestResult::getId)//防止格式化
                .eq(LabCriticalReport::getCriticalItemType, 4)
        );
        lambdaQueryWrapper.and(wrapper -> wrapper
                .eq(BacTestResult::getRptGroup, bacTestResult.getRptGroup())//防止格式化
                .eq(BacTestResult::getSampleNo, bacTestResult.getSampleNo())//防止格式化
                .ge(BacTestResult::getSampleda, startTime)//防止格式化
                .le(BacTestResult::getSampleda, endTime));//防止格式化
        lambdaQueryWrapper.and(wrapper -> wrapper.eq(BacTestResult::getIsAlarm, 1)//防止格式化
                .or(wrapper2 -> wrapper2.eq(BacTestResult::getIsDrugAlarm, 1)));
        lambdaQueryWrapper.eq(BacTestResult::getDelFlag, 0);
        List<BacTestResult> labRptresults = bacTestResultMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "成功", labRptresults);
    }

    @Override
    public Result findTestResultHistoryList(BacTestResult bacTestResult) {
        SysUser sysUser = RedisUserManager.getUser();
        //感染指标获取病人的检验结果信息
        if ("5".equals(bacTestResult.getVal2())) {
            //调用接口返回查询数据
            LambdaQueryWrapper<SysInterface> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysInterface::getDelFlag, 0);
            wrapper.eq(SysInterface::getIsEnable, 1);
            wrapper.eq(SysInterface::getOrgId, sysUser.getOrgId());
            List<SysInterface> sysInterfaces = sysInterfaceMapper.selectList(wrapper);
            List<String> collect = sysInterfaces.stream().filter(o -> Objects.nonNull(o) && org.apache.commons.lang3.StringUtils.isNotBlank(o.getInterfaceUrl())).map(SysInterface::getInterfaceUrl).distinct().collect(Collectors.toList());
            if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(collect)) {
                try {
                    /*发布报告相关*/
                    for (String webUrl : collect) {
                        String updateUrl = webUrl + "/public/GetBacInfectionGetRptResult";
                        Map<String, Object> updateMap = new HashMap<>();
                        updateMap.put("patNo", bacTestResult.getPatNo());
                        updateMap.put("orgId", sysUser.getOrgId());
                        String labRptResult = HttpUtil.createPost(updateUrl).setConnectionTimeout(3000).body(JSONUtil.toJsonStr(updateMap)).contentType("application/json").execute().body();
                        JSONObject jsonObject = JSONUtil.parseObj(labRptResult);
                        JSONArray data = jsonObject.getJSONArray("data");
                        List<LabRptresult> list = BeanUtil.copyToList(data, LabRptresult.class);
                        return Result.succ(1, "请求成功", list);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        //涂片的结果
        else {
            if ("4".equals(bacTestResult.getVal1())) {
                MPJLambdaWrapper<BacStainingResult> queryWrapper = new MPJLambdaWrapper<>();
                queryWrapper.selectAll(BacStainingResult.class);
                queryWrapper.selectAs(LabMaininfo::getTestPurpose, BacStainingResult::getTestPurpose);
                queryWrapper.selectAs(LabMaininfo::getReqPurpose, BacStainingResult::getReqPurpose);
                queryWrapper.selectAs(LabMaininfo::getIsChk, BacStainingResult::getIsChk);
                queryWrapper.selectAs(LabSampleType::getSampleTypeName, BacStainingResult::getTraits);
                queryWrapper.selectAs("chk", SysUser::getRealname, BacStainingResult::getChkDoctor);
                queryWrapper.selectAs(LabMaininfo::getChkTime, BacStainingResult::getChkTime);
//        queryWrapper.leftJoin(LabMaininfo.class, LabMaininfo::getId, BacTestResult::getMaininfoId);
                queryWrapper.leftJoin(LabMaininfo.class, on -> on.eq(LabMaininfo::getSampleda, BacStainingResult::getSampleda)
                        .eq(LabMaininfo::getSampleNo, BacStainingResult::getSampleNo)
                        .eq(LabMaininfo::getRptGroup, BacStainingResult::getRptGroup)
                );

                queryWrapper.leftJoin(SysUser.class, "chk", SysUser::getUsername, LabMaininfo::getChkDoctor);
                queryWrapper.leftJoin(LabSampleType.class, on -> on.eq(LabSampleType::getSampleTypeCode, LabMaininfo::getSampleType).eq(LabSampleType::getDelFlag, 0));
                queryWrapper.eq(BacStainingResult::getDelFlag, 0);
                queryWrapper.eq(LabMaininfo::getPatNo, bacTestResult.getPatNo());
                queryWrapper.eq(LabMaininfo::getRptGroup, bacTestResult.getRptGroup());
                queryWrapper.eq(BacStainingResult::getHandleType, 0);

                //多少天
                if (StrUtil.isNotEmpty(bacTestResult.getVal3())) {
                    queryWrapper.ge(BacStainingResult::getSampleda, DateUtil.offsetDay(bacTestResult.getSampleda(), -Integer.parseInt(bacTestResult.getVal3())));
                }
                switch (bacTestResult.getVal2()) {
                    //同类标本
                    case "2":
                        queryWrapper.eq(LabMaininfo::getSampleType, bacTestResult.getSampleType());
                        break;
                    //相同标本
                    case "3":
                        queryWrapper.eq(LabMaininfo::getSampleType, bacTestResult.getSampleType());
                        break;
                    //同天标本
                    case "4":
                        queryWrapper.eq(LabMaininfo::getSampleda, bacTestResult.getSampleda());
                        break;
                    //感染指标
                    case "5":
                        //相关检验
                    case "6":

                }
                queryWrapper.orderByDesc(BacTestResult::getCreateTime);
                List<BacStainingResult> bacStainingResults = bacStainingResultMapper.selectList(queryWrapper);
                return Result.succ(1, "请求成功", bacStainingResults);
            } else {
                MPJLambdaWrapper<BacTestResult> queryWrapper = new MPJLambdaWrapper<>();
                queryWrapper.selectAll(BacTestResult.class);
                queryWrapper.selectAs(LabMaininfo::getTestPurpose, BacTestResult::getTestPurpose);
                queryWrapper.selectAs(LabMaininfo::getReqPurpose, BacTestResult::getReqPurpose);
                queryWrapper.selectAs(LabSampleType::getSampleTypeName, BacTestResult::getTraits);
                queryWrapper.selectAs("chk", SysUser::getRealname, BacTestResult::getChkDoctor);
                queryWrapper.selectAs(LabMaininfo::getChkTime, BacTestResult::getChkTime);
                queryWrapper.selectAs(LabMaininfo::getIsChk, BacTestResult::getIsChk);
//        queryWrapper.leftJoin(LabMaininfo.class, LabMaininfo::getId, BacTestResult::getMaininfoId);
                queryWrapper.leftJoin(LabMaininfo.class, on -> on.eq(LabMaininfo::getSampleda, BacTestResult::getSampleda)
                        .eq(LabMaininfo::getSampleNo, BacTestResult::getSampleNo)
                        .eq(LabMaininfo::getRptGroup, BacTestResult::getRptGroup)
                );

                queryWrapper.leftJoin(SysUser.class, "chk", SysUser::getUsername, LabMaininfo::getChkDoctor);
                queryWrapper.leftJoin(LabSampleType.class, on -> on.eq(LabSampleType::getSampleTypeCode, LabMaininfo::getSampleType).eq(LabSampleType::getDelFlag, 0));
                queryWrapper.eq(BacTestResult::getDelFlag, 0);
                queryWrapper.eq(LabMaininfo::getPatNo, bacTestResult.getPatNo());
                queryWrapper.eq(LabMaininfo::getRptGroup, bacTestResult.getRptGroup());

                //多少天
                if (StrUtil.isNotEmpty(bacTestResult.getVal3())) {
                    queryWrapper.ge(BacTestResult::getSampleda, DateUtil.offsetDay(bacTestResult.getSampleda(), -Integer.parseInt(bacTestResult.getVal3())));
                }

                switch (bacTestResult.getVal1()) {
                    //阳性
                    case "2":
                        queryWrapper.isNotNull(BacTestResult::getGermNo);
                        break;
                    //阴性
                    case "3":
                        queryWrapper.isNull(BacTestResult::getGermNo);
                        break;
                    //染色
                    case "4":
//                queryWrapper.isNull(BacTestResult::getGermNo);
//                break;
                }

                switch (bacTestResult.getVal2()) {
                    //同类标本
                    case "2":
                        queryWrapper.eq(LabMaininfo::getSampleType, bacTestResult.getSampleType());
                        break;
                    //相同标本
                    case "3":
                        queryWrapper.eq(LabMaininfo::getSampleType, bacTestResult.getSampleType());
                        break;
                    //同天标本
                    case "4":
                        queryWrapper.eq(LabMaininfo::getSampleda, bacTestResult.getSampleda());
                        break;
                    //感染指标
                    case "5":
                        //相关检验
                    case "6":

                }
                queryWrapper.orderByDesc(BacTestResult::getCreateTime);
                List<BacTestResult> bacTestResults = bacTestResultMapper.selectList(queryWrapper);
                return Result.succ(1, "请求成功", bacTestResults);
            }
        }
        return Result.succ(1, "请求成功", "");
    }

    @Override
    public List<BacTestResult> findBacTestResultArray(BacTestResult bacTestResult) {
        MPJLambdaWrapper<BacTestResult> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacTestResult::getDelFlag, 0);
        if (!BeanUtil.isEmpty(bacTestResult.getSampleda())) {
            Timestamp startTime = Timestamp.valueOf(bacTestResult.getSampleda().toString().substring(0, 10) + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(bacTestResult.getSampleda().toString().substring(0, 10) + " 23:59:59");
            queryWrapper.ge(BacTestResult::getSampleda, startTime);
            queryWrapper.le(BacTestResult::getSampleda, endTime);
        }
        if (!BeanUtil.isEmpty(bacTestResult.getOperateDate())) {
            Timestamp startTime = Timestamp.valueOf(bacTestResult.getOperateDate().toString().substring(0, 10) + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(bacTestResult.getOperateDate().toString().substring(0, 10) + " 23:59:59");
            queryWrapper.ge(BacTestResult::getOperateDate, startTime);
            queryWrapper.le(BacTestResult::getOperateDate, endTime);
        }
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getRptGroup()), BacTestResult::getRptGroup, bacTestResult.getRptGroup());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getSampleNo()), BacTestResult::getSampleNo, bacTestResult.getSampleNo());
//        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getMaininfoId()), BacTestResult::getMaininfoId, bacTestResult.getMaininfoId());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getTestFlowNo()), BacTestResult::getTestFlowNo, bacTestResult.getTestFlowNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getTestNodeNo()), BacTestResult::getTestNodeNo, bacTestResult.getTestNodeNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getIsolateNum()), BacTestResult::getIsolateNum, bacTestResult.getIsolateNum());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getInfeSiteNo()), BacTestResult::getInfeSiteNo, bacTestResult.getInfeSiteNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getGermNo()), BacTestResult::getGermNo, bacTestResult.getGermNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getGermNa()), BacTestResult::getGermNa, bacTestResult.getGermNa());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getTestResult()), BacTestResult::getTestResult, bacTestResult.getTestResult());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getResiPhenNo()), BacTestResult::getResiPhenNo, bacTestResult.getResiPhenNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getUrinColoCount()), BacTestResult::getUrinColoCount, bacTestResult.getUrinColoCount());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getDescText()), BacTestResult::getDescText, bacTestResult.getDescText());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getOperateUserNo()), BacTestResult::getOperateUserNo, bacTestResult.getOperateUserNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestResult.getItemDictNo()), BacTestResult::getItemDictNo, bacTestResult.getItemDictNo());
        queryWrapper.like(StringUtils.isNotBlank(bacTestResult.getResiPhenNo()), BacTestResult::getResiPhenNo, bacTestResult.getResiPhenNo());
        queryWrapper.like(StringUtils.isNotBlank(bacTestResult.getResiPhenNa()), BacTestResult::getResiPhenNa, bacTestResult.getResiPhenNa());
        queryWrapper.like(StringUtils.isNotBlank(bacTestResult.getRemark1()), BacTestResult::getRemark1, bacTestResult.getRemark1());
        queryWrapper.like(StringUtils.isNotBlank(bacTestResult.getRemark2()), BacTestResult::getRemark2, bacTestResult.getRemark2());
        queryWrapper.like(StringUtils.isNotBlank(bacTestResult.getRemark3()), BacTestResult::getRemark3, bacTestResult.getRemark3());
        queryWrapper.like(StringUtils.isNotBlank(bacTestResult.getRemark4()), BacTestResult::getRemark4, bacTestResult.getRemark4());


        queryWrapper.orderByAsc(BacTestResult::getCreateTime);

        return bacTestResultMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacTestResult(BacTestResult bacTestResult) {
        int i = bacTestResultMapper.insert(bacTestResult);
        if (i > 0) {
            /*计算折点*/
            /*查询出细菌下的抗生素*/
            LambdaQueryWrapper<BacDrugTestResult> bacDrugTestResultLambdaQueryWrapper = new LambdaQueryWrapper<>();
            bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getDelFlag, 0);
            if (ToolsUtils.isNotEmpty(bacTestResult.getSampleda())) {
                Timestamp timestamp = new Timestamp(bacTestResult.getSampleda().getTime());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String formattedDate = sdf.format(timestamp);
                Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
                Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
                bacDrugTestResultLambdaQueryWrapper.ge(BacDrugTestResult::getSampleda, startTime);
                bacDrugTestResultLambdaQueryWrapper.le(BacDrugTestResult::getSampleda, endTime);
            }
            bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getIsolateNum, bacTestResult.getIsolateNum());
            bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getGermNo, bacTestResult.getGermNo());
            bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getRptGroup, bacTestResult.getRptGroup());
            bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getSampleNo, bacTestResult.getSampleNo());
            bacDrugTestResultLambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacTestResult.getTestFlowNo()), BacDrugTestResult::getTestFlowNo, bacTestResult.getTestFlowNo());
            bacDrugTestResultLambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacTestResult.getTestNodeNo()), BacDrugTestResult::getTestNodeNo, bacTestResult.getTestNodeNo());
            List<BacDrugTestResult> testResultList = bacDrugTestResultMapper.selectList(bacDrugTestResultLambdaQueryWrapper);
            if (CollectionUtils.isNotEmpty(testResultList)) {
                //判断是否是不出报告的药敏
                //先查出上机记录
                String format = DateUtil.format(bacTestResult.getSampleda(), "yyyy-MM-dd");
                Timestamp startTime1 = Timestamp.valueOf(format + " 00:00:00");
                Timestamp endTime1 = Timestamp.valueOf(format + " 23:59:59");
                LambdaQueryWrapper<LabMaininfo> queryWrapper3 = new LambdaQueryWrapper<>();
                queryWrapper3.eq(LabMaininfo::getDelFlag, 0)
                        .eq(LabMaininfo::getRptGroup, bacTestResult.getRptGroup())
                        .ge(LabMaininfo::getSampleda, startTime1)
                        .le(LabMaininfo::getSampleda, endTime1)
                        .eq(LabMaininfo::getSampleNo, bacTestResult.getSampleNo())
                        .eq(LabMaininfo::getOrgId, bacTestResult.getOrgId());
                List<LabMaininfo> maininfoList1 = labMaininfoMapper.selectList(queryWrapper3);

                for (BacDrugTestResult m : testResultList) {
                    String notReportSensitiveNo = null;
                    if (ToolsUtils.isNotEmpty(maininfoList1)) {
                        LabMaininfo labMaininfo = maininfoList1.get(0);
                        if (ToolsUtils.isNotEmpty(labMaininfo)) {
                            notReportSensitiveNo = bacDrugTestResultService.judgeDrugNotReport(m, labMaininfo);
                        }
                    }

                    //判断是否符合天然耐药规则
                    bacDrugTestResultService.judgeNaturalMdrRule(m);
                    Integer isNaturlResis = m.getIsNaturlResis();
                    //1是天然耐药
                    if (!(ToolsUtils.isNotEmpty(isNaturlResis) && isNaturlResis == 1) && ToolsUtils.isEmpty(notReportSensitiveNo)) {

                        boolean isCalcBP = false;/*重新赋值折点*/
                        boolean isCalcIC = false;/*重新计算敏感性*/
                        if (ToolsUtils.isNotEmpty(m.getDataFrom()) && "2".equals(m.getDataFrom())) {
                            isCalcBP = true;
                            isCalcIC = true;
                        } else if (ToolsUtils.isNotEmpty(m.getDataFrom()) && "1".equals(m.getDataFrom())) {
                            if (ToolsUtils.isNotEmpty(m.getIsBpFromInstr()) && m.getIsBpFromInstr().equals(1)) {
                                isCalcBP = false;
                            }
                            isCalcIC = false;

                            try {
                                SysParamSearchOutputDto paramResult = redisSysParamHelper.getValueByBGDYXTXXCSParam(m.getOrgId(), "instrDataIsCalcBP", m.getRptGroup(), null);
                                String wswymbgsfhbbg = paramResult.getValueStr();
                                if (ToolsUtils.isNotEmpty(wswymbgsfhbbg) && "1".equals(wswymbgsfhbbg)) {
                                    isCalcBP = true;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            try {
                                SysParamSearchOutputDto paramResult = redisSysParamHelper.getValueByBGDYXTXXCSParam(m.getOrgId(), "instrDataIsCalcIC", m.getRptGroup(), null);
                                String wswymbgsfhbbg = paramResult.getValueStr();
                                if (ToolsUtils.isNotEmpty(wswymbgsfhbbg) && "1".equals(wswymbgsfhbbg)) {
                                    isCalcIC = true;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }

                        //查看此结果是否符合药敏结果敏感性规则
                        String sensitiveNo = bacDrugTestResultService.judgeSensitiveResultRule(m, isCalcIC);
                        if (ToolsUtils.isEmpty(sensitiveNo)) {
                            bacBreakPointCalcService.setApplicableBreakpoint(bacTestResult, Arrays.asList(m), isCalcBP, isCalcIC);
                        }
                    }

                }
            }
            String rptGroup = bacTestResult.getRptGroup();
            String sampleNo = bacTestResult.getSampleNo();
            Timestamp sampleda = bacTestResult.getSampleda();

            Timestamp timestamp = new Timestamp(sampleda.getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            Integer isAlarmCnt = 0;
            //查询培养观察结果中是否有危急值数据
            MPJLambdaWrapper<BacObserResult> queryWrapper = new MPJLambdaWrapper<>();
            queryWrapper.eq(BacObserResult::getDelFlag, 0)
                    .eq(BacObserResult::getRptGroup, rptGroup)
                    .eq(BacObserResult::getSampleNo, sampleNo)
                    .ge(BacObserResult::getSampleda, startTime)
                    .le(BacObserResult::getSampleda, endTime)
                    .eq(BacObserResult::getIsAlarm, 1);
            Integer obsCnt = bacObserResultMapper.selectCount(queryWrapper);
            isAlarmCnt += obsCnt;

            //查询血培养结果中是否有危急值数据
            MPJLambdaWrapper<BacBloodCultrue> queryWrapperb = new MPJLambdaWrapper<>();
            queryWrapperb.eq(BacBloodCultrue::getDelFlag, 0)
                    .eq(BacBloodCultrue::getRptGroup, rptGroup)
                    .eq(BacBloodCultrue::getSampleNo, sampleNo)
                    .ge(BacBloodCultrue::getSampleda, startTime)
                    .le(BacBloodCultrue::getSampleda, endTime)
                    .eq(BacBloodCultrue::getIsAlarm, 1);
            Integer culCnt = bacBloodCultrueMapper.selectCount(queryWrapperb);
            isAlarmCnt += culCnt;

            //查询细菌结果中是否有危急值数据
            MPJLambdaWrapper<BacTestResult> queryWrapperR = new MPJLambdaWrapper<>();
            queryWrapperR.eq(BacTestResult::getDelFlag, 0)
                    .eq(BacTestResult::getRptGroup, rptGroup)
                    .eq(BacTestResult::getSampleNo, sampleNo)
                    .ge(BacTestResult::getSampleda, startTime)
                    .le(BacTestResult::getSampleda, endTime)
                    .eq(BacTestResult::getIsAlarm, 1);
            Integer rptCnt = bacTestResultMapper.selectCount(queryWrapperR);
            isAlarmCnt += rptCnt;


            LambdaUpdateWrapper<LabMaininfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(LabMaininfo::getRptGroup, rptGroup)
                    .eq(LabMaininfo::getSampleda, sampleda)
                    .eq(LabMaininfo::getSampleNo, sampleNo);
            if (isAlarmCnt > 0) {
                lambdaUpdateWrapper.set(LabMaininfo::getIsAlarm, 1);
            } else {
                lambdaUpdateWrapper.set(LabMaininfo::getIsAlarm, 0);
            }
            labMaininfoMapper.update(null, lambdaUpdateWrapper);

            return Result.succ(1, "新增成功", bacTestResult);
        }
        return Result.fail("新增失败");
    }

    @Autowired
    RedisSysParamHelper redisSysParamHelper;

    @Override
    public Result deleteBacTestResult(BacTestResult bacTestResult) {
        bacTestResult.setDelFlag(1);
        int i = bacTestResultMapper.updateById(bacTestResult);
        if (i > 0) {
            //删除抗生素
            String rptGroup = bacTestResult.getRptGroup();
            String sampleNo = bacTestResult.getSampleNo();
            String isolateNum = bacTestResult.getIsolateNum();
            String germNo = bacTestResult.getGermNo();
            //更新数据
            LambdaUpdateWrapper<BacDrugTestResult> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(BacDrugTestResult::getDelFlag, 1)
                    .eq(BacDrugTestResult::getRptGroup, rptGroup)
                    .eq(BacDrugTestResult::getSampleNo, sampleNo)
                    .eq(BacDrugTestResult::getTestFlowNo, bacTestResult.getTestFlowNo())
                    .eq(BacDrugTestResult::getTestNodeNo, bacTestResult.getTestNodeNo())
                    .eq(BacDrugTestResult::getIsolateNum, isolateNum)
                    .eq(BacDrugTestResult::getGermNo, germNo)
                    .eq(BacDrugTestResult::getDelFlag, 0);
            if (!BeanUtil.isEmpty(bacTestResult.getSampleda())) {
                Timestamp startTime = Timestamp.valueOf(bacTestResult.getSampleda().toString().substring(0, 10) + " 00:00:00");
                Timestamp endTime = Timestamp.valueOf(bacTestResult.getSampleda().toString().substring(0, 10) + " 23:59:59");
                lambdaUpdateWrapper.ge(BacDrugTestResult::getSampleda, startTime);
                lambdaUpdateWrapper.le(BacDrugTestResult::getSampleda, endTime);
            }
            bacDrugTestResultMapper.update(null, lambdaUpdateWrapper);
            return Result.succ(1, "删除成功", bacTestResult);
        }
        return Result.fail("删除失败");
    }

    @Autowired
    BacObserResultMapper bacObserResultMapper;
    @Autowired
    BacBloodCultrueMapper bacBloodCultrueMapper;

    @Override
    public Result updateBacTestResult(BacTestResult bacTestResult) {
        int i = bacTestResultMapper.updateById(bacTestResult);
        if (i > 0) {
            /*计算折点*/
            /*查询出细菌下的抗生素*/
            LambdaQueryWrapper<BacDrugTestResult> bacDrugTestResultLambdaQueryWrapper = new LambdaQueryWrapper<>();
            bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getDelFlag, 0);
            if (ToolsUtils.isNotEmpty(bacTestResult.getSampleda())) {
                Timestamp timestamp = new Timestamp(bacTestResult.getSampleda().getTime());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String formattedDate = sdf.format(timestamp);
                Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
                Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
                bacDrugTestResultLambdaQueryWrapper.ge(BacDrugTestResult::getSampleda, startTime);
                bacDrugTestResultLambdaQueryWrapper.le(BacDrugTestResult::getSampleda, endTime);
            }
            bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getIsolateNum, bacTestResult.getIsolateNum());
            //bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getGermNo, bacTestResult.getGermNo());
            bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getRptGroup, bacTestResult.getRptGroup());
            bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getSampleNo, bacTestResult.getSampleNo());
            bacDrugTestResultLambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacTestResult.getTestFlowNo()), BacDrugTestResult::getTestFlowNo, bacTestResult.getTestFlowNo());
            bacDrugTestResultLambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacTestResult.getTestNodeNo()), BacDrugTestResult::getTestNodeNo, bacTestResult.getTestNodeNo());
            List<BacDrugTestResult> testResultList = bacDrugTestResultMapper.selectList(bacDrugTestResultLambdaQueryWrapper);
            if (CollectionUtils.isNotEmpty(testResultList)) {
                List<BacDrugTestResult> updateDrugTestResultList = new ArrayList<>();
                //判断是否跟当前细菌的GERM_NO相同，如果不相同则需要修改药敏中的GERM_NO
                for (BacDrugTestResult m : testResultList) {
                    if (ToolsUtils.isNotEmpty(m.getGermNo()) && !m.getGermNo().equals(bacTestResult.getGermNo())) {
                        m.setGermNo(bacTestResult.getGermNo());
                        updateDrugTestResultList.add(m);
                    }
                }
                if (CollectionUtils.isNotEmpty(updateDrugTestResultList)) {
                    bacDrugTestResultService.updateBatchById(updateDrugTestResultList);
                }


                //判断是否是不出报告的药敏
                //先查出上机记录
                String format = DateUtil.format(bacTestResult.getSampleda(), "yyyy-MM-dd");
                Timestamp startTime1 = Timestamp.valueOf(format + " 00:00:00");
                Timestamp endTime1 = Timestamp.valueOf(format + " 23:59:59");
                LambdaQueryWrapper<LabMaininfo> queryWrapper3 = new LambdaQueryWrapper<>();
                queryWrapper3.eq(LabMaininfo::getDelFlag, 0)
                        .eq(LabMaininfo::getRptGroup, bacTestResult.getRptGroup())
                        .ge(LabMaininfo::getSampleda, startTime1)
                        .le(LabMaininfo::getSampleda, endTime1)
                        .eq(LabMaininfo::getSampleNo, bacTestResult.getSampleNo())
                        .eq(LabMaininfo::getOrgId, bacTestResult.getOrgId());
                List<LabMaininfo> maininfoList1 = labMaininfoMapper.selectList(queryWrapper3);
                for (BacDrugTestResult m : testResultList) {
                    String notReportSensitiveNo = null;
                    if (ToolsUtils.isNotEmpty(maininfoList1)) {
                        LabMaininfo labMaininfo = maininfoList1.get(0);
                        if (ToolsUtils.isNotEmpty(labMaininfo)) {
                            notReportSensitiveNo = bacDrugTestResultService.judgeDrugNotReport(m, labMaininfo);
                        }
                    }
                    //判断是否符合天然耐药规则
                    bacDrugTestResultService.judgeNaturalMdrRule(m);
                    Integer isNaturlResis = m.getIsNaturlResis();
                    //1是天然耐药
                    if (!(ToolsUtils.isNotEmpty(isNaturlResis) && isNaturlResis == 1) && ToolsUtils.isEmpty(notReportSensitiveNo)) {

                        boolean isCalcBP = false;/*重新赋值折点*/
                        boolean isCalcIC = false;/*重新计算敏感性*/
                        if (ToolsUtils.isNotEmpty(m.getDataFrom()) && "2".equals(m.getDataFrom())) {
                            isCalcBP = true;
                            isCalcIC = true;
                        } else if (ToolsUtils.isNotEmpty(m.getDataFrom()) && "1".equals(m.getDataFrom())) {
                            if (ToolsUtils.isNotEmpty(m.getIsBpFromInstr()) && m.getIsBpFromInstr().equals(1)) {
                                isCalcBP = false;
                            }
                            isCalcIC = false;

                            try {
                                SysParamSearchOutputDto paramResult = redisSysParamHelper.getValueByBGDYXTXXCSParam(m.getOrgId(), "instrDataIsCalcBP", m.getRptGroup(), null);
                                String wswymbgsfhbbg = paramResult.getValueStr();
                                if (ToolsUtils.isNotEmpty(wswymbgsfhbbg) && "1".equals(wswymbgsfhbbg)) {
                                    isCalcBP = true;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            try {
                                SysParamSearchOutputDto paramResult = redisSysParamHelper.getValueByBGDYXTXXCSParam(m.getOrgId(), "instrDataIsCalcIC", m.getRptGroup(), null);
                                String wswymbgsfhbbg = paramResult.getValueStr();
                                if (ToolsUtils.isNotEmpty(wswymbgsfhbbg) && "1".equals(wswymbgsfhbbg)) {
                                    isCalcIC = true;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }

                        //查看此结果是否符合药敏结果敏感性规则
                        String sensitiveNo = bacDrugTestResultService.judgeSensitiveResultRule(m, isCalcIC);
                        if (ToolsUtils.isEmpty(sensitiveNo)) {
                            bacBreakPointCalcService.setApplicableBreakpoint(bacTestResult, Arrays.asList(m), isCalcBP, isCalcIC);
                        }
                    }
                }
            }

            String rptGroup = bacTestResult.getRptGroup();
            String sampleNo = bacTestResult.getSampleNo();
            Timestamp sampleda = bacTestResult.getSampleda();

            Timestamp timestamp = new Timestamp(sampleda.getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            Integer isAlarmCnt = 0;
            //查询培养观察结果中是否有危急值数据
            MPJLambdaWrapper<BacObserResult> queryWrapper = new MPJLambdaWrapper<>();
            queryWrapper.eq(BacObserResult::getDelFlag, 0)
                    .eq(BacObserResult::getRptGroup, rptGroup)
                    .eq(BacObserResult::getSampleNo, sampleNo)
                    .ge(BacObserResult::getSampleda, startTime)
                    .le(BacObserResult::getSampleda, endTime)
                    .eq(BacObserResult::getIsAlarm, 1);
            Integer obsCnt = bacObserResultMapper.selectCount(queryWrapper);
            isAlarmCnt += obsCnt;

            //查询血培养结果中是否有危急值数据
            MPJLambdaWrapper<BacBloodCultrue> queryWrapperb = new MPJLambdaWrapper<>();
            queryWrapperb.eq(BacBloodCultrue::getDelFlag, 0)
                    .eq(BacBloodCultrue::getRptGroup, rptGroup)
                    .eq(BacBloodCultrue::getSampleNo, sampleNo)
                    .ge(BacBloodCultrue::getSampleda, startTime)
                    .le(BacBloodCultrue::getSampleda, endTime)
                    .eq(BacBloodCultrue::getIsAlarm, 1);
            Integer culCnt = bacBloodCultrueMapper.selectCount(queryWrapperb);
            isAlarmCnt += culCnt;

            //查询细菌结果中是否有危急值数据
            MPJLambdaWrapper<BacTestResult> queryWrapperR = new MPJLambdaWrapper<>();
            queryWrapperR.eq(BacTestResult::getDelFlag, 0)
                    .eq(BacTestResult::getRptGroup, rptGroup)
                    .eq(BacTestResult::getSampleNo, sampleNo)
                    .ge(BacTestResult::getSampleda, startTime)
                    .le(BacTestResult::getSampleda, endTime)
                    .eq(BacTestResult::getIsAlarm, 1);
            Integer rptCnt = bacTestResultMapper.selectCount(queryWrapperR);
            isAlarmCnt += rptCnt;


            LambdaUpdateWrapper<LabMaininfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(LabMaininfo::getRptGroup, rptGroup)
                    .eq(LabMaininfo::getSampleda, sampleda)
                    .eq(LabMaininfo::getSampleNo, sampleNo);
            if (isAlarmCnt > 0) {
                lambdaUpdateWrapper.set(LabMaininfo::getIsAlarm, 1);
            } else {
                lambdaUpdateWrapper.set(LabMaininfo::getIsAlarm, 0);
            }
            labMaininfoMapper.update(null, lambdaUpdateWrapper);

            return Result.succ(1, "更新成功", bacTestResult);
        }
        return Result.fail("更新失败");
    }

    @Override
    public Result findTestResultAndDrugList(BacTestResult bacTestResult) {

        //判断bacTestResult是否为空,如果不为空则查询出关联的BAC_DRUG_RESULT表的数据,并组装成树形结构
        if (ToolsUtils.isNotEmpty(bacTestResult)) {
            List<BacDrugTestResult> bacDrugTestResultList = bacDrugTestResultMapper.selectList(new LambdaQueryWrapper<BacDrugTestResult>()
                    .eq(ToolsUtils.isNotEmpty(bacTestResult.getRptGroup()), BacDrugTestResult::getRptGroup, bacTestResult.getRptGroup())
                    .eq(ToolsUtils.isNotEmpty(bacTestResult.getSampleNo()), BacDrugTestResult::getSampleNo, bacTestResult.getSampleNo())
                    .eq(ToolsUtils.isNotEmpty(bacTestResult.getSampleda()), BacDrugTestResult::getSampleda, bacTestResult.getSampleda())
                    .eq(ToolsUtils.isNotEmpty(bacTestResult.getTestFlowNo()), BacDrugTestResult::getTestFlowNo, bacTestResult.getTestFlowNo())
                    .eq(ToolsUtils.isNotEmpty(bacTestResult.getTestNodeNo()), BacDrugTestResult::getTestNodeNo, bacTestResult.getTestNodeNo())
                    .eq(ToolsUtils.isNotEmpty(bacTestResult.getGermNo()), BacDrugTestResult::getGermNo, bacTestResult.getGermNo())
                    .eq(ToolsUtils.isNotEmpty(bacTestResult.getIsolateNum()), BacDrugTestResult::getIsolateNum, bacTestResult.getIsolateNum())
                    .eq(BacDrugTestResult::getDelFlag, 0));

            return Result.succ(bacDrugTestResultList);
        } else {
            return Result.fail("查询失败");
        }
    }

    @Override
    public Result syncTestResultAndDrugList(BacDrugDto dto) {
        //病人信息
        LabMaininfo labMaininfo = dto.getLabMaininfo();
        //细菌信息
        BacTestResult germResult = dto.getGermResult();
        //历史细菌信息
        BacTestResult bacTestResult = dto.getBacTestResult();
        //历史细菌药敏信息
        List<BacDrugTestResult> bacDrugTestResultList = dto.getBacDrugTestResultList();

        //判断germResult和bacTestResult是否是同一细菌
        if (bacTestResult.getGermNo().equals(germResult.getGermNo())) {

            germResult.setTestResult(bacTestResult.getTestResult());
            germResult.setDescText(bacTestResult.getDescText());

            this.saveOrUpdate(germResult);

            //是,直接同步药敏
            if (ToolsUtils.isNotEmpty(bacDrugTestResultList)) {
                //List<BacDrugTestResult> list = new ArrayList<>();
                for (BacDrugTestResult m : bacDrugTestResultList) {
                    //同步过来的结果不允许再次同步
                    // if (ToolsUtils.isNotEmpty(m.getIsSyncResult()) && m.getIsSyncResult() == 1) {
                    //     continue;
                    // }
                    BacDrugTestResult bacDrugTestResult = new BacDrugTestResult();
                    BeanUtil.copyProperties(m, bacDrugTestResult, "id", "sampleda", "rptGroup", "sampleNo", "testFlowNo", "testNodeNo", "isolateNum");
                    bacDrugTestResult.setSampleda(labMaininfo.getSampleda());
                    bacDrugTestResult.setSampleNo(labMaininfo.getSampleNo());
                    bacDrugTestResult.setRptGroup(labMaininfo.getRptGroup());
                    bacDrugTestResult.setTestFlowNo(germResult.getTestFlowNo());
                    bacDrugTestResult.setTestNodeNo(germResult.getTestNodeNo());
                    bacDrugTestResult.setIsolateNum(germResult.getIsolateNum());
                    bacDrugTestResult.setIsSyncResult(1);
                    //list.add(bacDrugTestResult);

                    LambdaQueryWrapper<BacDrugTestResult> bacDrugTestResultLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getRptGroup, labMaininfo.getRptGroup())
                            .eq(BacDrugTestResult::getSampleNo, labMaininfo.getSampleNo())
                            .eq(BacDrugTestResult::getSampleda, labMaininfo.getSampleda())
                            .eq(BacDrugTestResult::getTestFlowNo, germResult.getTestFlowNo())
                            .eq(BacDrugTestResult::getTestNodeNo, germResult.getTestNodeNo())
                            .eq(BacDrugTestResult::getIsolateNum, germResult.getIsolateNum())
                            .eq(BacDrugTestResult::getGermNo, germResult.getGermNo())
                            .eq(BacDrugTestResult::getAntNo, m.getAntNo())
                            .eq(BacDrugTestResult::getDelFlag, 0);

                    bacDrugTestResultService.saveOrUpdate(bacDrugTestResult, bacDrugTestResultLambdaQueryWrapper);
                }
                //bacDrugTestResultService.saveOrUpdateBatch(list);
            }
            return Result.succ(1, "更新成功", null);
        } else {
            //同时同步细菌信息和药敏信息
            if (ToolsUtils.isNotEmpty(bacTestResult)) {

                LambdaQueryWrapper<BacTestWorkFlow> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(BacTestWorkFlow::getRptGroup, labMaininfo.getRptGroup())
                        .eq(BacTestWorkFlow::getSampleNo, labMaininfo.getSampleNo())
                        .eq(BacTestWorkFlow::getSampleda, labMaininfo.getSampleda())
                        .eq(BacTestWorkFlow::getReqNo, labMaininfo.getReqNo())
                        .eq(BacTestWorkFlow::getDelFlag, 0);

                BacTestWorkFlow bacTestWorkFlow = bacTestWorkFlowMapper.selectOne(queryWrapper);

                LambdaQueryWrapper<BacTestResult> bacTestResultLambdaQueryWrapper = new LambdaQueryWrapper<>();

                if (!BeanUtil.isEmpty(labMaininfo.getSampleda())) {
                    Timestamp startTime = Timestamp.valueOf(labMaininfo.getSampleda().toString().substring(0, 10) + " 00:00:00");
                    Timestamp endTime = Timestamp.valueOf(labMaininfo.getSampleda().toString().substring(0, 10) + " 23:59:59");
                    bacTestResultLambdaQueryWrapper.ge(BacTestResult::getSampleda, startTime);
                    bacTestResultLambdaQueryWrapper.le(BacTestResult::getSampleda, endTime);
                }
                bacTestResultLambdaQueryWrapper.eq(BacTestResult::getSampleNo, labMaininfo.getSampleNo())
                        .eq(BacTestResult::getRptGroup, labMaininfo.getRptGroup())
                        .eq(BacTestResult::getDelFlag, 0);

                List<BacTestResult> bacTestResults = bacTestResultMapper.selectList(bacTestResultLambdaQueryWrapper);

                //判断bacTestResults里是否包含germResult

                if (CollectionUtils.isNotEmpty(bacTestResults)) {
                    Optional<BacTestResult> first = bacTestResults.stream()
                            .filter(result -> bacTestResult.getGermNo().equals(result.getGermNo()))
                            .findFirst();

                    BacTestResult testResult;
                    if (first.isPresent()) {
                        testResult = first.get();
                        testResult.setTestResult(bacTestResult.getTestResult());
                        testResult.setDescText(bacTestResult.getDescText());
                    } else {
                        int lateNum = bacTestResults.size() + 1;

                        testResult = new BacTestResult();
                        BeanUtil.copyProperties(bacTestResult, testResult, "id", "sampleda", "rptGroup", "sampleNo", "testFlowNo", "testNodeNo", "isolateNum");
                        testResult.setSampleda(labMaininfo.getSampleda());
                        testResult.setSampleNo(labMaininfo.getSampleNo());
                        testResult.setRptGroup(labMaininfo.getRptGroup());
                        testResult.setTestFlowNo(bacTestWorkFlow.getWorkFlowNo());
                        testResult.setTestNodeNo("JINGJIAN");
                        testResult.setIsolateNum("Def" + Integer.toString(lateNum));
                        bacTestResultMapper.insert(testResult);
                    }

                    if (ToolsUtils.isNotEmpty(bacDrugTestResultList)) {
                        // List<BacDrugTestResult> list = new ArrayList<>();
                        for (BacDrugTestResult drugTestResult : bacDrugTestResultList) {
                            //同步过来的结果不允许再次同步
                            // if (ToolsUtils.isNotEmpty(drugTestResult.getIsSyncResult()) && drugTestResult.getIsSyncResult() == 1) {
                            //     continue;
                            // }
                            BacDrugTestResult bacDrugTestResult = new BacDrugTestResult();
                            BeanUtil.copyProperties(drugTestResult, bacDrugTestResult, "id", "sampleda", "rptGroup", "sampleNo", "testFlowNo", "testNodeNo", "isolateNum");
                            bacDrugTestResult.setSampleda(labMaininfo.getSampleda());
                            bacDrugTestResult.setSampleNo(labMaininfo.getSampleNo());
                            bacDrugTestResult.setRptGroup(labMaininfo.getRptGroup());
                            bacDrugTestResult.setTestFlowNo(bacTestWorkFlow.getWorkFlowNo());
                            bacDrugTestResult.setTestNodeNo("JINGJIAN");
                            bacDrugTestResult.setIsolateNum(testResult.getIsolateNum());
                            bacDrugTestResult.setIsSyncResult(1);

                            LambdaQueryWrapper<BacDrugTestResult> bacDrugTestResultLambdaQueryWrapper = new LambdaQueryWrapper<>();
                            bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getRptGroup, labMaininfo.getRptGroup())
                                    .eq(BacDrugTestResult::getSampleNo, labMaininfo.getSampleNo())
                                    .eq(BacDrugTestResult::getSampleda, labMaininfo.getSampleda())
                                    .eq(BacDrugTestResult::getTestFlowNo, bacTestWorkFlow.getWorkFlowNo())
                                    .eq(BacDrugTestResult::getTestNodeNo, "JINGJIAN")
                                    .eq(BacDrugTestResult::getIsolateNum, testResult.getIsolateNum())
                                    .eq(BacDrugTestResult::getGermNo, testResult.getGermNo())
                                    .eq(BacDrugTestResult::getAntNo, drugTestResult.getAntNo())
                                    .eq(BacDrugTestResult::getDelFlag, 0);

                            bacDrugTestResultService.saveOrUpdate(bacDrugTestResult, bacDrugTestResultLambdaQueryWrapper);
                        }
                        //bacDrugTestResultService.saveOrUpdateBatch(list);
                    }
                    return Result.succ(1, "更新成功", null);
                } else {
                    BacTestResult testResult = new BacTestResult();
                    BeanUtil.copyProperties(bacTestResult, testResult, "id", "sampleda", "rptGroup", "sampleNo", "testFlowNo", "testNodeNo", "isolateNum");
                    testResult.setSampleda(labMaininfo.getSampleda());
                    testResult.setSampleNo(labMaininfo.getSampleNo());
                    testResult.setRptGroup(labMaininfo.getRptGroup());
                    testResult.setTestFlowNo(bacTestWorkFlow.getWorkFlowNo());
                    testResult.setTestNodeNo("JINGJIAN");
                    testResult.setIsolateNum("1");
                    bacTestResultMapper.insert(testResult);

                    if (ToolsUtils.isNotEmpty(bacDrugTestResultList)) {
                        // List<BacDrugTestResult> list = new ArrayList<>();
                        for (BacDrugTestResult m : bacDrugTestResultList) {
                            //同步过来的结果不允许再次同步
                            if (ToolsUtils.isNotEmpty(m.getIsSyncResult()) && m.getIsSyncResult() == 1) {
                                continue;
                            }
                            BacDrugTestResult bacDrugTestResult = new BacDrugTestResult();
                            BeanUtil.copyProperties(m, bacDrugTestResult, "id", "sampleda", "rptGroup", "sampleNo", "testFlowNo", "testNodeNo", "isolateNum");
                            bacDrugTestResult.setSampleda(labMaininfo.getSampleda());
                            bacDrugTestResult.setSampleNo(labMaininfo.getSampleNo());
                            bacDrugTestResult.setRptGroup(labMaininfo.getRptGroup());
                            bacDrugTestResult.setTestFlowNo(bacTestWorkFlow.getWorkFlowNo());
                            bacDrugTestResult.setTestNodeNo("JINGJIAN");
                            bacDrugTestResult.setIsolateNum("1");
                            bacDrugTestResult.setIsSyncResult(1);

                            LambdaQueryWrapper<BacDrugTestResult> bacDrugTestResultLambdaQueryWrapper = new LambdaQueryWrapper<>();
                            bacDrugTestResultLambdaQueryWrapper.eq(BacDrugTestResult::getRptGroup, labMaininfo.getRptGroup())
                                    .eq(BacDrugTestResult::getSampleNo, labMaininfo.getSampleNo())
                                    .eq(BacDrugTestResult::getSampleda, labMaininfo.getSampleda())
                                    .eq(BacDrugTestResult::getTestFlowNo, bacTestWorkFlow.getWorkFlowNo())
                                    .eq(BacDrugTestResult::getTestNodeNo, "JINGJIAN")
                                    .eq(BacDrugTestResult::getIsolateNum, "1")
                                    .eq(BacDrugTestResult::getGermNo, testResult.getGermNo())
                                    .eq(BacDrugTestResult::getAntNo, m.getAntNo())
                                    .eq(BacDrugTestResult::getDelFlag, 0);

                            bacDrugTestResultService.saveOrUpdate(bacDrugTestResult, bacDrugTestResultLambdaQueryWrapper);
                        }
                        //bacDrugTestResultService.saveOrUpdateBatch(list);
                    }
                }

            }
            return Result.succ(1, "更新成功", null);
        }

    }

    @Autowired
    SysBasDictDetailService sysBasDictDetailService;

    @Override
    public Result findPositiveSample(BacTestCountDto bacTestCountDto) {
        MPJLambdaWrapper<LabMaininfo> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(LabMaininfo.class);
        if (bacTestCountDto.getIsDistinct() == 1) {
            mpjLambdaWrapper.distinct();
        }
        mpjLambdaWrapper.selectAs(BacTestResult::getGermNa, LabMaininfo::getGermNa)
                .selectAs(LabSampleType::getSampleTypeName, LabMaininfo::getSampleTypeNa)
                .selectAs(SysOrgDepartment::getName, LabMaininfo::getReqDeptNa)
                .selectAs("req", PubEmployee::getName, LabMaininfo::getReqDoctorNa)
                .innerJoin(BacTestResult.class, p -> p.eq(BacTestResult::getRptGroup, LabMaininfo::getRptGroup).eq(BacTestResult::getSampleda, LabMaininfo::getSampleda).eq(BacTestResult::getSampleNo, LabMaininfo::getSampleNo))
                .innerJoin(LabRptGroup.class, p -> p.eq(LabRptGroup::getRptGroup, BacTestResult::getRptGroup).eq(LabRptGroup::getBusinessType, 20))
                .leftJoin(BacGermDict.class, p -> p.eq(BacTestResult::getGermNo, BacGermDict::getGermNo))
                .leftJoin(LabSampleType.class, on -> on.eq(LabSampleType::getSampleTypeCode, LabMaininfo::getSampleType).eq(LabSampleType::getDelFlag, 0))
                .leftJoin(SysOrgDepartment.class, on -> on.eq(SysOrgDepartment::getCode, LabMaininfo::getReqDept).eq(SysOrgDepartment::getDelFlag, 0))
                .leftJoin(PubEmployee.class, "req", on -> on.eq(PubEmployee::getCode, LabMaininfo::getReqDoctor).eq(PubEmployee::getDelFlag, 0))
                .eq(BacTestResult::getDelFlag, 0);
        mpjLambdaWrapper.eq(StrUtil.isNotEmpty(bacTestCountDto.getRptGroup()), BacTestResult::getRptGroup, bacTestCountDto.getRptGroup());
        mpjLambdaWrapper.eq(StrUtil.isNotEmpty(bacTestCountDto.getReqDept()), LabMaininfo::getReqDept, bacTestCountDto.getReqDept());
        mpjLambdaWrapper.between(StrUtil.isNotEmpty(bacTestCountDto.getStartTime()) && StrUtil.isNotEmpty(bacTestCountDto.getEndTime()), LabMaininfo::getSampleda, bacTestCountDto.getStartTime(), bacTestCountDto.getEndTime());
        mpjLambdaWrapper.eq(StrUtil.isNotEmpty(bacTestCountDto.getSampleType()), LabMaininfo::getSampleType, bacTestCountDto.getSampleType());
        mpjLambdaWrapper.eq(StrUtil.isNotEmpty(bacTestCountDto.getGermType()), BacGermDict::getGermType, bacTestCountDto.getGermType());
        mpjLambdaWrapper.eq(StrUtil.isNotEmpty(bacTestCountDto.getBacteria()), BacGermDict::getGermNo, bacTestCountDto.getBacteria());
        // mpjLambdaWrapper.eq(bacTestCountDto.getIsAlarm() == 1, BacTestResult::getIsAlarm, 1);
        mpjLambdaWrapper.isNotNull(BacTestResult::getGermNo);
        mpjLambdaWrapper.eq(LabMaininfo::getIsChk, BigDecimal.valueOf(2));
        List<LabMaininfo> maininfoList = labMaininfoMapper.selectList(mpjLambdaWrapper);
        if (CollectionUtils.isEmpty(maininfoList)) {
            return Result.fail("暂无数据");
        }

        List<Map<String, Object>> patTypeList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("PATTYPE").getData();
        for (LabMaininfo maininfo : maininfoList) {
            if (org.apache.commons.lang3.StringUtils.isNotBlank(maininfo.getPatType()) && org.apache.commons.collections4.CollectionUtils.isNotEmpty(patTypeList)) {
                patTypeList.stream().filter(a -> a.get("code").equals(maininfo.getPatType())).findFirst().ifPresent(stringObjectMap -> maininfo.setPatTypeNa((String) stringObjectMap.get("name")));
            }
        }


        return Result.succ(1, "查询成功", maininfoList);
    }
}


